package com.yi.mall.goods.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.yi.mall.common.model.*;
import com.yi.mall.common.model.DTO.ProductDetailDTO;
import com.yi.mall.goods.mapper.*;
import com.yi.mall.goods.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service

public class ProductServiceImpl implements IProductService {
    @Autowired
    private SpuInfoMapper spuInfoMapper;
    @Autowired
    private SpuSaleAttrMapper spuSaleAttrMapper;
    @Autowired
    private SpuSaleAttrValueMapper spuSaleAttrValueMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SkuInfoMapper skuInfoMapper;
    @Override
    public ProductDetailDTO queryById(Long id) {
        ProductDetailDTO spuInfoDTO = new ProductDetailDTO();
        // 获取Spu
        SpuInfo spuInfo = spuInfoMapper.selectById(id);
        if(spuInfo != null){
            // 获取spu类别
            List<String> categoryNames = getCategoryNames(spuInfo.getCategoryId());
            // 获取类别的spu销售属性
            Map<String,List<String>> attrMap = getSpuSaleAttrs(id);
            // 获取商品SKU信息
            List<SkuInfo> skuInfoList = listSkuInfo(id);
            // 封装数据
            spuInfoDTO = new ProductDetailDTO(categoryNames,attrMap,skuInfoList);
        }
        return spuInfoDTO;
    }

    /**
     * 获取商品类别
     */
    private List<String> getCategoryNames(Long categoryId){
        List<String> names = new ArrayList<>();
        // 获取三级类别名称
        Category three = categoryMapper.selectById(categoryId);
        if(three != null){
            names.add(three.getName());
            // 获取二级类别名称
            Category two = categoryMapper.selectById(three.getParentId());
            if(two != null){
                names.add(0,two.getName());
                // 获取一级类别名称
                Category one = categoryMapper.selectById(two.getParentId());
                if(one != null){
                    names.add(0,one.getName());
                }
            }
        }
        return names;
    }
    /**
     * 获取类别的spu属性
     */
    private  Map<String,List<String>> getSpuSaleAttrs(Long spuId){
        // 获取spuid的所有属性
        LambdaQueryChainWrapper<SpuSaleAttr> spuAttrWrapper = new LambdaQueryChainWrapper<>(spuSaleAttrMapper);
        List<SpuSaleAttr> attrs = spuAttrWrapper.eq(SpuSaleAttr::getSpuId, spuId)
                .select(SpuSaleAttr::getBaseSaleAttrId, SpuSaleAttr::getSaleAttrName)
                .list();
        // 获取spuid的所有属性值
        LambdaQueryChainWrapper<SpuSaleAttrValue> spuValueWrapper = new LambdaQueryChainWrapper<>(spuSaleAttrValueMapper);
        List<SpuSaleAttrValue> values = spuValueWrapper.eq(SpuSaleAttrValue::getSpuId, spuId)
                .select(SpuSaleAttrValue::getBaseSaleAttrId, SpuSaleAttrValue::getSaleAttrValueName)
                .list();
        // 封装属性
        Map<String,List<String>> attrMaps =new HashMap<>();
        for (SpuSaleAttr attr : attrs) {
            String k = attr.getSaleAttrName();
            List<String> v = new ArrayList<>();
            for (SpuSaleAttrValue value : values) {
                if(attr.getBaseSaleAttrId() == value.getBaseSaleAttrId()){
                    v.add(value.getSaleAttrValueName());
                }
            }
            attrMaps.put(k,v);
        }
        return  attrMaps;
    }

    /**
     * 获取SPU的SKU集合
     */
    private List<SkuInfo> listSkuInfo(Long spuId){
        LambdaQueryChainWrapper<SkuInfo> wrapper = new LambdaQueryChainWrapper<>(skuInfoMapper);
        wrapper.eq(SkuInfo::getSpuId,spuId);
        return wrapper.list();
    }
}
